package com.niit.service.impl;

import com.niit.entity.Notification;
import com.niit.entity.NotificationRead;
import com.niit.entity.User;
import com.niit.entity.embeddable.NotificationReadId;
import com.niit.repository.NotificationReadRepository;
import com.niit.repository.NotificationRepository;
import com.niit.repository.UserRepository;
import com.niit.service.NotificationService;
import com.niit.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class NotificationServiceImpl implements NotificationService {
    @Autowired
    private NotificationRepository notificationRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private NotificationReadRepository notificationReadRepository;

    @Override
    public Page<Notification> getNotifications(String title, Byte level, Byte type, Pageable pageable, Integer loginType) {
        // 根据登录类型确定可见的通知级别
        List<Byte> levels = new ArrayList<>();
        if(level != null) levels.add(level);
        else levels = getVisibleLevels(loginType);

        Page<Notification> pageData;
        if(title != null && type != null){
            pageData = notificationRepository.findByTitleContainingAndLevelInAndType(title, levels, type, pageable);
        }else if(title != null){
            pageData = notificationRepository.findByTitleContainingAndLevelIn(title, levels, pageable);
        }else if(type != null){
            pageData = notificationRepository.findByLevelInAndType(levels, type, pageable);
        }else {
            pageData = notificationRepository.findByLevelIn(levels, pageable);
        }
        return pageData;
    }

    @Override
    public Notification getNotificationById(Integer id) {
        return notificationRepository.findById(id).orElse(null);
    }

    private List<Byte> getVisibleLevels(Integer loginType) {
        List<Byte> levels = new ArrayList<>();
        if (loginType == 3) { // 管理员
            levels.add((byte) 1);
            levels.add((byte) 2);
            levels.add((byte) 3);
            levels.add((byte) 4);
        } else if (loginType == 1) { // 求职者
            levels.add((byte) 2);
            levels.add((byte) 4);
        } else if (loginType == 2) { // 招聘者
            levels.add((byte) 3);
            levels.add((byte) 4);
        }
        return levels;
    }

    @Override
    public void createNotification(Notification notification) {
        notification.setCreateTime(LocalDateTime.now());
        notificationRepository.save(notification);
    }

    @Override
    public void updateNotification(Integer id, Notification notification) {
        if (notificationRepository.existsById(id)) {
            notification.setId(id);
            notificationRepository.save(notification);
        }
    }

    @Override
    @Transactional
    public void deleteNotification(Integer id) {
        notificationRepository.deleteById(id);
    }

    @Override
    @Transactional
    public void deleteNotifications(List<Integer> ids) {
        notificationRepository.deleteAllById(ids);
    }

    @Override
    @Transactional
    public void markAsRead(Integer userId, Integer notificationId) {
        Optional<User> user = userRepository.findById(userId);
        Optional<Notification> notification = notificationRepository.findById(notificationId);

        if (user.isPresent() && notification.isPresent()) {
            NotificationRead readRecord = new NotificationRead();
            NotificationReadId notificationReadId = new NotificationReadId();
            notificationReadId.setNotificationId(notificationId);
            notificationReadId.setUserId(userId);
            readRecord.setId(notificationReadId);
            readRecord.setUser(user.get());
            readRecord.setNotification(notification.get());
            readRecord.setReadTime(LocalDateTime.now());
            notificationReadRepository.save(readRecord);
        }
    }
}